import router from '@/router'
// import { getUserInfo } from '@/api/me'
import { getToken, setToken, removeToken } from '@/utils/auth'

const getUserInfo = async () => {}

const state = () => ({
  token: getToken(),
  userInfo: {}
})

const mutations = {
  SET_USERINFO (state, userInfo) {
    state.userInfo = userInfo
  },
  SET_TOKEN (state, token) {
    setToken(token)
    state.token = token
  }
}

const actions = {
  getUesrInfo ({ commit }) {
    return new Promise((resolve, reject) => {
      return getUserInfo()
        .then(res => {
          const userInfo = res.userInfo

          commit('SET_USERINFO', userInfo)
          resolve(userInfo)
        })
        .catch(reject)
    })
  },
  goSso ({ commit }) {
    commit('SET_TICKET', '')
    removeToken()
    router.replace('/401')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
